package com.belf.evaluatetool.service.impl;

import belf.migrate.api.job.Job;
import belf.migrate.api.taskconf.SourceConf;
import belf.migrate.api.taskconf.Task;
import belf.migrate.api.taskconf.TaskConf;
import belf.migrate.engine.job.JobResult;
import belf.migrate.engine.job.Result;
import belf.migrate.engine.job.evaluate.EvaluateJobRunner;
import belf.migrate.evaluate.model.EvaluateObjectsResult;
import belf.migrate.evaluate.model.EvaluateResult;
import belf.migrate.evaluate.model.EvaluateWholeResult;
import com.belf.evaluatetool.domain.EvalTaskConf;
import com.belf.evaluatetool.service.EvalService;
import com.google.gson.JsonObject;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

import static belf.migrate.api.taskconf.Task.TaskType.EVALUATE_MIGRATION;

/**
 * EvalServiceImpl
 *
 * @author l
 * @since 2024/10/28 17:47
 */

@Service
public class EvalServiceImpl implements EvalService {

    @Override
    public Result testEval(EvalTaskConf evalTaskConf) {
        Job job = getJob(evalTaskConf);
        try {
            JobResult execute = new EvaluateJobRunner(job).execute();
            HashMap<String, Object> resultMap = parseResult(execute);
            return Result.success(resultMap);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 构建任务参数对象
     * @param evalTaskConf
     * @return
     */
    private Job getJob(EvalTaskConf evalTaskConf){
        Job job = new Job();
        job.setJobId(1L);
        job.setCreateTime(new Date());
            Task task = new Task();
            task.setTaskType(EVALUATE_MIGRATION);
            task.setTaskPolicy(Task.TaskPolicy.ERROR_CONTINUE);
            task.setTaskId(1);
            task.setName("test");
            task.setDesc("test");
            task.setCreateDate(new Date());
        TaskConf taskConf = new TaskConf();
        taskConf.setTaskType(evalTaskConf.getTaskType());
        taskConf.setCreateDate(new Date());
        taskConf.setId(1);
        taskConf.setTaskName(evalTaskConf.getTaskType());
        SourceConf sourceConf = evalTaskConf.getSource();
        new DatabaseServiceImpl().buildUrl(sourceConf);
        taskConf.setSourceConf(sourceConf);
        task.setTaskConf(taskConf);
        job.setTask(task);
        return job;
    }

    private HashMap<String, Object>  parseResult(JobResult execute){
        EvaluateResult data = (EvaluateResult)execute.getData();
        EvaluateWholeResult wholeResult = data.getWholeResult();
        Double dataSize = wholeResult.getDataSize();
        Integer allTableRows = wholeResult.getAllTableRows();

        EvaluateObjectsResult objectsResult = data.getObjectsResult();
        Integer allTableSize = objectsResult.getTableResult() == null ? null : objectsResult.getTableResult().getAllObjectSize();
        Integer viewCount = objectsResult.getViewResult()== null ? null : objectsResult.getViewResult().getAllObjectSize();
        Integer functionCount = objectsResult.getFunctionResult()== null ? null : objectsResult.getFunctionResult().getAllObjectSize();
        Integer triggerCount = objectsResult.getTriggerResult()== null ? null : objectsResult.getTriggerResult().getAllObjectSize();
        Integer producedCount = objectsResult.getProcedureResult()== null ? null : objectsResult.getProcedureResult().getAllObjectSize();
        Integer synonymCount = objectsResult.getSynonymResult()== null ? null : objectsResult.getSynonymResult().getAllObjectSize();
        Integer seqCount = objectsResult.getSeqResult()== null ? null : objectsResult.getSeqResult().getAllObjectSize();
        Integer indexCount = objectsResult.getIndexResult()== null ? null : objectsResult.getIndexResult().getAllObjectSize();
        Integer typeCount = objectsResult.getTypeResult()== null ? null : objectsResult.getTypeResult().getAllObjectSize();
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("dataSize", dataSize);
        resultMap.put("allTableRows", allTableRows);
        resultMap.put("allTableSize", allTableSize);
        resultMap.put("viewCount", viewCount);
        resultMap.put("functionCount", functionCount);
        resultMap.put("triggerCount", triggerCount);
        resultMap.put("producedCount", producedCount);
        resultMap.put("synonymCount", synonymCount);
        resultMap.put("seqCount", seqCount);
        resultMap.put("indexCount", indexCount);
        resultMap.put("typeCount", typeCount);
        return resultMap;
    }
}
